﻿//==============================================================================
// File: MessageBoxViewPresentationModel.cs
// Created: 2010-05-28
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using WpfRcp.Core.Services;
using WpfRcp.Core.Views;
using WpfRcp.Core.Views.Presenters;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace WpfRcp.Views.Presenters
{
    public class MessageBoxViewPresentationModel : PresenterBase<IMessageBoxView>, IMessageBox
    {
        private readonly IShell m_Shell;
        private readonly IRcpSettings m_RcpSettings;

        private const string ErrorIcon = "ErrorIcon";
        private const string WarningIcon = "WarningIcon";
        private const string AcceptIcon = "AcceptIcon";
        private const string InfoIcon = "InfoIcon";
        private const string HelpIcon = "HelpIcon";

        private MessageBoxViewResult m_Result;
        private bool m_ButtonOkVisible;
        private bool m_ButtonCancelVisible;
        private bool m_ButtonYesVisible;
        private bool m_ButtonNoVisible;
        private string m_Title;
        private string m_Message;

        public DelegateCommand<object> OkCommand { get; private set;}
        
        public DelegateCommand<object> YesCommand { get; private set; }
        
        public DelegateCommand<object> NoCommand { get; private set; }
        
        public DelegateCommand<object> CancelCommand { get; private set; }

        public MessageBoxViewPresentationModel(IMessageBoxView view, IShell shell, IRcpSettings rcpSettings)
        {
            m_Shell = shell;
            m_RcpSettings = rcpSettings;
            View = view;
            View.PresentationModel = this;

            // Setup commands
            OkCommand = new DelegateCommand<object>(ExecuteOkCommand);
            YesCommand = new DelegateCommand<object>(ExecuteYesCommand);
            NoCommand = new DelegateCommand<object>(ExecuteNoCommand);
            CancelCommand = new DelegateCommand<object>(ExecuteCancelCommand);

            // Define defaults
            SetupButtons(MessageBoxViewButtons.OK);
            View.SetImage(InfoIcon);
        }

        public bool ButtonOkVisible
        {
            get { return m_ButtonOkVisible; }
            set
            {
                m_ButtonOkVisible = value;
                OnPropertyChanged("ButtonOkVisible");
            }
        }

        public bool ButtonCancelVisible
        {
            get { return m_ButtonCancelVisible; }
            set
            {
                m_ButtonCancelVisible = value;
                OnPropertyChanged("ButtonCancelVisible");
            }
        }

        public bool ButtonNoVisible
        {
            get { return m_ButtonNoVisible; }
            set
            {
                m_ButtonNoVisible = value;
                OnPropertyChanged("ButtonNoVisible");
            }
        }

        public bool ButtonYesVisible
        {
            get { return m_ButtonYesVisible; }
            set
            {
                m_ButtonYesVisible = value;
                OnPropertyChanged("ButtonYesVisible");
            }
        }

        public string Title
        {
            get { return m_Title; }
            set
            {
                m_Title = value;
                OnPropertyChanged("Title");
            }
        }

        public string Message
        {
            get { return m_Message; }
            set
            {
                m_Message = value;
                OnPropertyChanged("Message");
            }
        }

        public IRcpSettings RcpSettings
        {
            get { return m_RcpSettings; }
        }

        public MessageBoxViewResult Show(string title, string message)
        {
            Title = title;
            Message = message;

            View.ShowDialog();

            return m_Result;
        }

        public MessageBoxViewResult Show(string title, string message, MessageBoxViewButtons buttons)
        {
            Title = title;
            Message = message;

            SetupButtons(buttons);

            View.ViewOwner = m_Shell;
            View.ShowDialog();

            return m_Result;
        }

        public MessageBoxViewResult Show(string title, string message, MessageBoxViewButtons buttons, MessageBoxViewIcons icons)
        {
            Title = title;
            Message = message;

            SetupButtons(buttons);
            SetupIcon(icons);

            View.ViewOwner = m_Shell;
            View.ShowDialog();

            return m_Result;
        }

        public MessageBoxViewResult Show(object owner, string title, string message, MessageBoxViewButtons buttons)
        {
            Title = title;
            Message = message;

            SetupButtons(buttons);
            View.ViewOwner = owner;
            View.ShowDialog();

            return m_Result;
        }

        public MessageBoxViewResult Show(object owner, string title, string message, MessageBoxViewButtons buttons, MessageBoxViewIcons icons)
        {
            Title = title;
            Message = message;

            SetupButtons(buttons);
            SetupIcon(icons);
            View.ViewOwner = owner;
            View.ShowDialog();

            return m_Result;
        }

        private void SetupButtons(MessageBoxViewButtons buttons)
        {
            if (buttons == MessageBoxViewButtons.OK)
            {
                ButtonOkVisible = true;
                ButtonNoVisible = false;
                ButtonYesVisible = false;
                ButtonCancelVisible = false;
                
                m_Result = MessageBoxViewResult.None;
            }
            else if (buttons == MessageBoxViewButtons.OKCancel)
            {
                ButtonOkVisible = true;
                ButtonNoVisible = false;
                ButtonYesVisible = false;
                ButtonCancelVisible = true;
                
                m_Result = MessageBoxViewResult.Cancel;
            }
            else if (buttons == MessageBoxViewButtons.YesNo)
            {
                ButtonOkVisible = false;
                ButtonNoVisible = true;
                ButtonYesVisible = true;
                ButtonCancelVisible = false;

                m_Result = MessageBoxViewResult.No;
            }
            else
            {
                ButtonOkVisible = false;
                ButtonNoVisible = true;
                ButtonYesVisible = true;
                ButtonCancelVisible = true;

                m_Result = MessageBoxViewResult.Cancel;
            }
        }

        private void SetupIcon(MessageBoxViewIcons icons)
        {
            switch (icons)
            {
                case MessageBoxViewIcons.Done:
                    View.SetImage(AcceptIcon);
                    break;

                case MessageBoxViewIcons.Error:
                    View.SetImage(ErrorIcon);
                    break;

                case MessageBoxViewIcons.Information:
                    View.SetImage(InfoIcon);
                    break;

                case MessageBoxViewIcons.Question:
                    View.SetImage(HelpIcon);
                    break;

                case MessageBoxViewIcons.Warning:
                    View.SetImage(WarningIcon);
                    break;
            }
        }

        private void ExecuteOkCommand(object args)
        {
            m_Result = MessageBoxViewResult.OK;
            View.Close();
            m_Shell.Focus();
        }

        private void ExecuteYesCommand(object args)
        {
            m_Result = MessageBoxViewResult.Yes;
            View.Close();
            m_Shell.Focus();
        }

        private void ExecuteNoCommand(object args)
        {
            m_Result = MessageBoxViewResult.No;
            View.Close();
            m_Shell.Focus();
        }

        private void ExecuteCancelCommand(object args)
        {
            m_Result = MessageBoxViewResult.Cancel;
            View.Close();
            m_Shell.Focus();
        }
    }
}